package com.qqbot.impl.listener;

import com.qqbot.core.annotation.GroupUserAddHandler;
import com.qqbot.core.core.Group;
import com.qqbot.core.core.Member;
import com.qqbot.core.injector.object.UserAddMessage;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.DuJiaoShouApiService;
import com.qqbot.impl.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 事件监听
 *
 * @author changwenpeng
 * @date 2022年5月9日
 */
@Component
public class EventListener {

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    DuJiaoShouApiService duJiaoShouApiService;

    /**
     * 入群欢迎
     *
     * @param group
     * @param um
     * @throws Exception
     */
    @GroupUserAddHandler(groupIds = {727289809, 478728208})
    public void userAdd(Group group, UserAddMessage um) throws Exception {
        MessageChain mc = new MessageChain();
        Long userId = um.getUserId();
        Member member = group.getMember(userId);
        mc.text("热烈欢迎【" + member.getNickname() + "】")
                .text("加入我们【" + group.getGroupName() + "】来了就别走了啊")
                .face(randomId()).face(randomId()).face(randomId())
                .face(randomId()).face(randomId()).face(randomId())
                .add(duJiaoShouApiService.head(userId.toString()));
        if(group.getGroupId() == 478728208){
            String key = group.getGroupId() + "" + userId;
            redisUtils.add(key, System.currentTimeMillis());
        }
        group.sendMessage(mc);

    }

    /**
     * 生成随机数
     *
     * @return
     */
    private String randomId() {
        return (int) (Math.random() * 220 + 1) + "";
    }
}
